package com.zenchn.controller;


import com.zenchn.annotation.OperLog;
import com.zenchn.entity.HttpDataResult;
import com.zenchn.enums.OperLogActionEnum;
import com.zenchn.exception.CheckException;
import com.zenchn.model.TSysDictType;
import com.zenchn.model.TSysDictTypeExample;
import com.zenchn.service.DictTypeService;
import com.zenchn.utils.CommonUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 数据字典类型模块控制器
 *
 * @author dingxinfa
 * @date 2020-04-20
 **/

@RestController
@RequestMapping(value = "dictType")
@Api(value = "数据字典类型模块相关接口",tags="系统管理-数据字典类型")
@RequiresPermissions("data:dict")
public class DictTypeController {


    private final String MODULE="系统管理>数据字典";

    @Resource
    private DictTypeService dictTypeService;

    /**
     * 数据字典类型数据列表
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @OperLog(module=MODULE,action= OperLogActionEnum.QUERY,detail="获取全部功能权限列表")
    @ApiOperation(value = "数据字典类型数据列表", notes = "数据字典类型数据列表")
    @ApiImplicitParam(dataType = "Boolean", name = "isEnable", value = "是否启用")
    @GetMapping(value = "/list")
    public ResponseEntity<HttpDataResult<List<TSysDictType>>> list(
            @RequestParam(required = false, value = "isEnable") Boolean isEnable,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpDataResult<List<TSysDictType>> result = new HttpDataResult<>(request.getRequestURI());
        TSysDictTypeExample example=new TSysDictTypeExample();
        if (null!=isEnable){
            example.createCriteria().andIsEnableEqualTo(isEnable);
        }

        List<TSysDictType> dictTypeList=dictTypeService.selectByExample(example);

        result.setData(dictTypeList);

        result.setMessage("数据字典类型列表成功.");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }



    /**
     * 新增数据字典类型保存提交
     * @param dictType
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @OperLog(module=MODULE,action= OperLogActionEnum.INSERT,detail="新增数据字典类型保存提交")
    @ApiOperation(value = "新增数据字典类型保存提交", notes = "新增数据字典类型保存提交")
    @ApiParam(type = "TSysDictType", name = "dictType", value = "数据字典类型对象", required = true)
    @PostMapping(value = "/addSubmit")
    public ResponseEntity<HttpDataResult<String>> addSubmit(
            @RequestBody @Valid TSysDictType dictType, BindingResult results,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpDataResult<String> result = new HttpDataResult<>(request.getRequestURI());
        if (results.hasErrors()){
            throw new CheckException(Objects.requireNonNull(results.getFieldError()).getDefaultMessage());
        }

        dictType.setDictTypeId(CommonUtils.randomUUID());
        dictTypeService.insert(dictType);

        result.setData(dictType.getDictTypeId());

        result.setMessage("新增数据字典类型成功。");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);

    }


    /***
     * 获取数据字典类型信息详情
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @OperLog(module=MODULE,action= OperLogActionEnum.QUERY,detail="获取数据字典类型信息详情")
    @ApiOperation(value = "获取数据字典类型信息详情", notes = "获取数据字典类型信息详情")
    @ApiImplicitParam(dataType = "String", name = "dictTypeId", value = "数据字典类型ID", required = true)
    public ResponseEntity<HttpDataResult<TSysDictType>> getDictType(
            @RequestParam(value = "dictTypeId") String dictTypeId,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpDataResult<TSysDictType> result = new HttpDataResult<>(request.getRequestURI());

        TSysDictType dictType=dictTypeService.selectByPrimaryKey(dictTypeId);

        result.setData(dictType);
        result.setMessage("OK");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    /**
     * 修改数据字典类型保存提交
     * @param dictType
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @OperLog(module=MODULE,action= OperLogActionEnum.UPDATE,detail="修改数据字典类型保存提交")
    @ApiOperation(value = "修改数据字典类型保存提交", notes = "修改数据字典类型保存提交")
    @ApiParam( type = "TSysDictType",name = "dictType", value = "数据字典类型对象", required = true)
    @PutMapping(value = "/modifySubmit")
    public ResponseEntity<HttpDataResult<String>> modifySubmit(
            @RequestBody @Valid TSysDictType dictType, BindingResult results,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpDataResult<String> result = new HttpDataResult<>(request.getRequestURI());

        if (results.hasErrors()){
            throw new CheckException(Objects.requireNonNull(results.getFieldError()).getDefaultMessage());
        }
        dictTypeService.updateByPrimaryKeySelective(dictType);

        result.setData(dictType.getDictTypeId());

        result.setMessage("修改数据字典类型成功。");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


    /***
     * 数据字典类型启动停用变更
     * @param dictTypeId
     * @param isEnable
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @OperLog(module=MODULE,action= OperLogActionEnum.UPDATE,detail="数据字典类型启动停用变更",isSaveRequestData=true)
    @ApiOperation(value = "数据字典类型启动停用变更", notes = "数据字典类型启动停用变更")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "dictTypeId", value = "字典类型ID", required = true),
            @ApiImplicitParam(dataType = "Boolean", name = "isEnable", value = "是否启用", required = true)
    })
    @PutMapping(value = "/statusChange")
    public ResponseEntity<HttpDataResult<TSysDictType>> statusChange(
            @RequestParam(value = "dictTypeId") String dictTypeId,
            @RequestParam( value = "isEnable") Boolean isEnable,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpDataResult<TSysDictType> result = new HttpDataResult<>(request.getRequestURI());
        TSysDictType dictType= new TSysDictType();

        dictType.setDictTypeId(dictTypeId);
        dictType.setIsEnable(isEnable);

        dictTypeService.updateByPrimaryKeySelective(dictType);
        result.setData(dictType);

        result.setMessage("Ok.");
        result.setStatus(HttpStatus.OK.value());
        return new ResponseEntity<>(result, HttpStatus.OK);

    }
}
